package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.dao.UserDao;
import com.itheima.health.pojo.Menu;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author likepei
 * @Date 2020/3/26 2:51
 * @Version v1.0
 * @Description 用户业务接口实现类
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    //依赖注入
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    /**
     * 基于用户名 获取User对象
     *
     * @param username
     * @return
     */
    @Override
    public User findByUsername(String username) {
        //通过 用户名获取用户信息
        User user = userDao.findByUsername(username);
        //通过 用户名id  获取所关联的角色集合
        Set<Role> roleSet = roleDao.findByUserId(user.getId());
        user.setRoles(roleSet);
        //通过 每一个角色id  获取所关联的权限集合
        for (Role role : roleSet) {
            Set<Permission> permissionSet = permissionDao.findByRoleId(role.getId());
            role.setPermissions(permissionSet);
        }

        return user;
    }

    @Override
    public boolean login(String username, String password) {
        System.out.println("service_provide=======u:" + username + " p:" + password);
        if ("admin".equals(username) && "123".equals(password)) {
            return true;
        }
        return false;
    }

    @Override
    public List<Menu> findMenu(String username) {
        List<Menu> menuList = new ArrayList<>();

        List<Menu> menuList3 = new ArrayList<>();
        List<Menu> menuList1 = userDao.findMenu(username);
        for (Menu menu1 : menuList1) {
            if (menu1.getParentMenuId() == null) {
                menuList.add(menu1);
            } else {
                menuList3.add(menu1);
            }
        }
        for (Menu menu2 : menuList) {
            List<Menu> menuList2 = new ArrayList<>();
            for (Menu menu : menuList3) {
                if (menu.getParentMenuId().equals(menu2.getId())) {
                    menuList2.add(menu);
                }
            }
            menu2.setChildren(menuList2);
        }
        for (Menu menu : menuList) {
            System.out.println(menu);
        }
        return menuList;
    }

    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //调用dao, 获取检查项数据
        Page<User> page = userDao.selectByCondition(queryPageBean.getQueryString());
        //创建PageResult结果对象( 检查项的总记录数, 当前页显示需要的10条检查项数据 )
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(User user, Integer[] roleIds) {
        userDao.add(user);//存用户信息
        //存中间表数据
        for (Integer roleId : roleIds) {
            Map map = new HashMap();
            map.put("userId",user.getId());
            map.put("roleId",roleId);
            userDao.addUserAndRole(map);
        }
    }

    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    @Override
    public List<Integer> findroleIdsByuserId(Integer id) {
        return userDao.findroleIdsByuserId(id);
    }

    @Override
    public void edit(User user, Integer[] roleIds) {
        userDao.edit(user);//跟新用户信息
        userDao.deleteUserAndRole(user.getId());//删除旧的关联信息
            //建立新的联系
        for (Integer roleId : roleIds) {
            Map map = new HashMap();
            map.put("userId",user.getId());
            map.put("roleId",roleId);
            userDao.addUserAndRole(map);
        }
    }

    @Override
    public void deleteRoleIdByUserId(Integer id) {
        userDao.deleteRoleIdByUserId(id);
    }

    @Override
    public void deleteById(Integer id) {
userDao.deleteById(id);
    }


}
